home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-11-08 | 14.2 KB | 463 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: FWGC.cpp
- // Release Version: $ 1.0d11 $
- //
- // Copyright: © 1993, 1995 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #include "FWOS.hpp"
-
- #ifndef FWGC_H
- #include "FWGC.h"
- #endif
-
- #ifndef FWGRUTIL_H
- #include "FWGrUtil.h"
- #endif
-
- #ifndef FWGRGLOB_H
- #include "FWGrGlob.h"
- #endif
-
- #ifndef FWFXMATH_H
- #include "FWFxMath.h"
- #endif
-
- #ifndef FWREGION_H
- #include "FWRegion.h"
- #endif
-
- #ifndef FWGDEV_H
- #include "FWGDev.h"
- #endif
-
- #ifndef FWRASTER_H
- #include "FWRaster.h"
- #endif
-
- #ifndef FWMAPING_H
- #include "FWMaping.h"
- #endif
-
- #ifndef FWODGEOM_H
- #include "FWODGeom.h"
- #endif
-
- // ----- OpenDoc Includes -----
-
- #ifndef SOM_ODTransform_xh
- #include <Trnsform.xh>
- #endif
-
- #ifndef SOM_ODShape_xh
- #include <Shape.xh>
- #endif
-
- #ifndef SOM_ODCanvas_xh
- #include <Canvas.xh>
- #endif
-
- //========================================================================================
- // RunTime Info
- //========================================================================================
-
- #if FW_LIB_EXPORT_PRAGMAS
- #pragma lib_export on
- #endif
-
- #ifdef FW_BUILD_MAC
- #pragma segment FW_GRTransformContextSegment
- #endif
-
- //========================================================================================
- // class FW_CGraphicContext
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_CGraphicContext::FW_CGraphicContext
- //----------------------------------------------------------------------------------------
-
- FW_CGraphicContext::FW_CGraphicContext(Environment* ev) :
- fInitialized(FALSE),
- fPreviousGraphicContext(FW_gLastGC),
- fRasterizer(NULL),
- fDevicePreviousState(NULL),
- fDeviceSuspendState(NULL),
- fEnvironment(ev),
- fTransform(NULL)
- {
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CGraphicContext::InitGraphicContext
- //
- // Exception Handling Notes
- // Don't forget kids, if we fail during construction then our destructor *won't*
- // get called - i.e. CloseDevice won't be called to match the OpenDevice, etc.
- // We need to protect ourselves from things like SetClip failing (and yes, even stupid
- // little clip operations *do* fail).
- // We could use helper objects, but it seems easier to use nested try/catch blocks in
- // this case since we'd have no opportunity to reuse the helper classes.
- //----------------------------------------------------------------------------------------
- // clipShape is in local coordinate
-
- FW_DECLARE_THROW_POINT (FW_CGraphicContext_InitGraphicContext);
-
- void FW_CGraphicContext::InitGraphicContext(FW_CGraphicDevice* graphicDevice,
- ODTransform* transform,
- ODShape* clipShape)
- {
- FW_ASSERT(graphicDevice != NULL);
-
- if (fPreviousGraphicContext)
- fPreviousGraphicContext->PrivSuspend();
-
- FW_TRY
- {
- // ----- Save the transform ----
- fTransform = transform;
- if(fTransform != NULL)
- {
- #ifdef FW_BUILD_WIN
- fTransform->IncrementRefCount(fEnvironment);
- #endif
- #ifdef FW_BUILD_MAC
- fTransform->Acquire(fEnvironment);
- #endif
- }
-
- FW_TRY
- {
- // ------ Use the default rasterizer -----
- fRasterizer = FW_gRasterizer;
-
- // ----- Set the graphic device -----
- fGraphicDevice = graphicDevice;
- fGraphicDevice->Acquire();
-
- FW_TRY
- {
- // ----- Open the Device -----
- fDevicePreviousState = fGraphicDevice->OpenDevice(fEnvironment, this);
-
- FW_TRY
- {
- FW_CHECK_THROW_POINT (FW_CGraphicContext_InitGraphicContext);
-
- // ----- Set the clip -----
- SetClip(clipShape);
- }
- FW_CATCH_BEGIN
- FW_CATCH_EVERYTHING ()
- {
- fGraphicDevice->CloseDevice (fEnvironment, fDevicePreviousState);
- FW_THROW_SAME ();
- }
- FW_CATCH_END
- }
- FW_CATCH_BEGIN
- FW_CATCH_EVERYTHING ()
- {
- fGraphicDevice->Release ();
-
- FW_THROW_SAME ();
- }
- FW_CATCH_END
- }
- FW_CATCH_BEGIN
- FW_CATCH_EVERYTHING ()
- {
- if (fTransform != NULL)
- fTransform->Release(fEnvironment);
- FW_THROW_SAME ();
- }
- FW_CATCH_END
-
- }
- FW_CATCH_BEGIN
- FW_CATCH_EVERYTHING ()
- {
- if (fPreviousGraphicContext)
- fPreviousGraphicContext->PrivResume();
- FW_THROW_SAME ();
- }
- FW_CATCH_END
-
- FW_gLastGC = this;
- fInitialized = TRUE;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CGraphicContext::TerminateGraphicContext
- //----------------------------------------------------------------------------------------
-
- void FW_CGraphicContext::TerminateGraphicContext()
- {
- if(fInitialized)
- {
- // ----- Close the Device -----
- if (fDevicePreviousState)
- fGraphicDevice->CloseDevice(fEnvironment, fDevicePreviousState);
-
- // ----- Now we can release the device -----
- fGraphicDevice->Release();
- fGraphicDevice = NULL;
-
- // ----- and the transform -----
- if(fTransform != NULL)
- fTransform->Release(fEnvironment);
-
- // ----- Reset the last Graphic Context Global -----
- FW_gLastGC = fPreviousGraphicContext;
-
- if (fPreviousGraphicContext)
- fPreviousGraphicContext->PrivResume();
-
- fInitialized = FALSE;
- }
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CGraphicContext::~FW_CGraphicContext
- //----------------------------------------------------------------------------------------
-
- FW_CGraphicContext::~FW_CGraphicContext()
- {
- FW_START_DESTRUCTOR
-
- TerminateGraphicContext();
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CGraphicContext::PrivSuspend
- //----------------------------------------------------------------------------------------
-
- ODPlatformCanvas FW_CGraphicContext::GetPlatformCanvas() const
- {
- return fGraphicDevice->GetPlatformCanvas();
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CGraphicContext::PrivSuspend
- //----------------------------------------------------------------------------------------
-
- void FW_CGraphicContext::PrivSuspend()
- {
- fDeviceSuspendState = fGraphicDevice->Suspend();
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CGraphicContext::PrivResume
- //----------------------------------------------------------------------------------------
-
- void FW_CGraphicContext::PrivResume()
- {
- fGraphicDevice->Resume(fDeviceSuspendState);
- fDeviceSuspendState = NULL;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CGraphicContext::SetMapping
- //----------------------------------------------------------------------------------------
-
- void FW_CGraphicContext::SetMapping(const FW_CMapping& newMapping)
- {
- fMapping = newMapping;
- fGraphicDevice->MappingChanged();
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CGraphicContext::GetMapping
- //----------------------------------------------------------------------------------------
- void FW_CGraphicContext::GetMapping(FW_CMapping& mapping) const
- {
- mapping = fMapping;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CGraphicContext::GetClip
- //----------------------------------------------------------------------------------------
-
- ODShape* FW_CGraphicContext::GetClip() const
- {
- ODRgnHandle clipRegion = fGraphicDevice->GetClip();
- ODShape* clipShape = ::FW_NewODShape(fEnvironment, clipRegion);
- ODShape* clipShapeCopy = DeviceToLogical(clipShape);
- clipShape->Release(fEnvironment); // Disposes the region, too
- return clipShapeCopy;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CGraphicContext::SetClip
- //----------------------------------------------------------------------------------------
- // a copy of clipShape is used
-
- void FW_CGraphicContext::SetClip(ODShape* clipShape)
- {
- FW_ASSERT(clipShape != NULL);
-
- ODShape* clipShapeCopy = LogicalToDevice(clipShape);
- // fMapping.ContentToDevice(fEnvironment, clipShape, fGraphicDevice, fTransform);
- ODRgnHandle clipRegion = ::FW_GetShapeRegion(fEnvironment, clipShapeCopy);
- fGraphicDevice->SetClip(clipRegion); // Uses a copy
- clipShapeCopy->Release(fEnvironment); // Disposes the region, too
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CGraphicContext::GetClipRect
- //----------------------------------------------------------------------------------------
-
- void FW_CGraphicContext::GetClipRect(FW_CRect& clipRect) const
- {
- FW_SPlatformRect plfmClipRect;
- fGraphicDevice->GetClipRect(plfmClipRect);
- clipRect = DeviceToLogical(plfmClipRect);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CGraphicContext::SetClipRect
- //----------------------------------------------------------------------------------------
-
- void FW_CGraphicContext::SetClipRect(const FW_CRect& clipRect)
- {
- FW_SPlatformRect plfmClipRect = LogicalToDevice(clipRect);
- fGraphicDevice->SetClipRect(plfmClipRect);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CGraphicContext::SetRasterizer
- //----------------------------------------------------------------------------------------
-
- void FW_CGraphicContext::SetRasterizer(FW_CRasterizer* rasterizer)
- {
- fRasterizer = rasterizer;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CGraphicContext::LogicalToDevice
- //----------------------------------------------------------------------------------------
-
- FW_SPlatformPoint FW_CGraphicContext::LogicalToDevice(FW_CFixed xSize, FW_CFixed ySize) const
- {
- FW_CRect rect(FW_IntToFixed(0), FW_IntToFixed(0), xSize, ySize);
- FW_SPlatformRect plfmRect = LogicalToDevice(rect);
- return FW_SPlatformPoint(plfmRect.right - plfmRect.left, plfmRect.bottom - plfmRect.top);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CGraphicContext::LogicalToDevice
- //----------------------------------------------------------------------------------------
-
- FW_SPlatformPoint FW_CGraphicContext::LogicalToDevice(const FW_CPoint& point) const
- {
- FW_SPlatformPoint plfmPoint;
- fMapping.LogicalToDevice(fEnvironment, point, plfmPoint, fGraphicDevice, fTransform);
- return plfmPoint;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CGraphicContext::LogicalToDevice
- //----------------------------------------------------------------------------------------
-
- FW_SPlatformRect FW_CGraphicContext::LogicalToDevice(const FW_CRect& rect) const
- {
- FW_SPlatformRect plfmRect;
- fMapping.LogicalToDevice(fEnvironment, rect, plfmRect, fGraphicDevice, fTransform);
- return plfmRect;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CGraphicContext::LogicalToDevice
- //----------------------------------------------------------------------------------------
-
- ODShape* FW_CGraphicContext::LogicalToDevice(ODShape* shape) const
- {
- return fMapping.LogicalToDevice(fEnvironment, shape, fGraphicDevice, fTransform);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CGraphicContext::DeviceToLogical
- //----------------------------------------------------------------------------------------
-
- FW_CPoint FW_CGraphicContext::DeviceToLogical(short xSize, short ySize) const
- {
- FW_SPlatformRect plfmRect(0, 0, xSize, ySize);
- FW_CRect rect = DeviceToLogical(plfmRect);
- return FW_CPoint(rect.right - rect.left, rect.bottom - rect.top);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CGraphicContext::DeviceToLogical
- //----------------------------------------------------------------------------------------
-
- FW_CPoint FW_CGraphicContext::DeviceToLogical(const FW_SPlatformPoint& point) const
- {
- FW_CPoint pt;
- fMapping.DeviceToLogical(fEnvironment, point, pt, fGraphicDevice, fTransform);
- return pt;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CGraphicContext::DeviceToLogical
- //----------------------------------------------------------------------------------------
-
- FW_CRect FW_CGraphicContext::DeviceToLogical(const FW_SPlatformRect& rect) const
- {
- FW_CRect odRect;
- fMapping.DeviceToLogical(fEnvironment, rect, odRect, fGraphicDevice, fTransform);
- return odRect;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CGraphicContext::DeviceToLogical
- //----------------------------------------------------------------------------------------
-
- ODShape* FW_CGraphicContext::DeviceToLogical(ODShape* shape) const
- {
- return fMapping.DeviceToLogical(fEnvironment, shape, fGraphicDevice, fTransform);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CGraphicContext::GetOriginOffset
- //----------------------------------------------------------------------------------------
-
- FW_SPlatformPoint FW_CGraphicContext::GetOriginOffset() const
- {
- return fMapping.GetOriginOffset(fEnvironment, fGraphicDevice, fTransform);
- }
-
- //========================================================================================
- // class FW_CSaveRestoreContext
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_CSaveRestoreContext::FW_CSaveRestoreContext
- //----------------------------------------------------------------------------------------
-
- FW_CSaveRestoreContext::FW_CSaveRestoreContext(FW_CGraphicContext& gc) :
- fGC(gc),
- fSuspendResume(NULL)
- {
- FW_ASSERT(&gc == FW_gLastGC);
-
- fSuspendResume = gc.GetGraphicDevice()->Suspend();
-
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CSaveRestoreContext::FW_CSaveRestoreContext
- //----------------------------------------------------------------------------------------
-
- FW_CSaveRestoreContext::~FW_CSaveRestoreContext()
- {
- FW_START_DESTRUCTOR
-
- // ----- ATTENTION: I don't owned fSuspendResume so I don't delete it -----
- if (fSuspendResume)
- fGC.GetGraphicDevice()->Resume(fSuspendResume);
- }
-